<template>
  <div class="my-bill-content">
    授权页面
  </div>
</template>
<script>
  import common from '../common/common';
  import { login } from '../api/login/index';
  export default {
    data(){
      return {}
    },
    async mounted() {
      let userInfo = common.store.getUser();
      if (!userInfo) {
        const code = this.GetQueryString('code');
        if (code) {
          const res1 = await login({code}).catch(() => {
            this.$toast('网络出了点小差，请稍后重试');
          });
          if (res1 && res1.data && res1.data.code === 200) {
            // 保存用户信息
            openid = res1.data.data.openid;
            common.store.setUser(openid);
            this.$router.push('/home');
          } else {
            this.$toast('提示', '网络出了点小差，请稍后重试');
          }
        } else {
          const appid = 'wx838cf54dd030d93a';
          const redirect_uri = encodeURIComponent('http://www.zxskill.com');
          const scope = 'snsapi_userinfo';
          // const href = `https://open.weixin.qq.com/connect/qrconnect?appid=wx45326f31cb86bf50&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
          const href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=success#wechat_redirect`;
          console.log(href);
          window.location.href = href;
        }
      } else {
        this.$router.push('/home');
      }
    },
    methods: {
      GetQueryString(name) {
        const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
        const r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
      }
    }
  }
</script>

<style scoped lang="less" type="text/less">

</style>
